package com.itboye.app.mian.activity;

import java.text.SimpleDateFormat;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.data.model.MeTouGuoDeBean;
import com.itboye.jmsfhz.data.presenter.Projectpresenter;
import com.itboye.jmsfhz.widget.XListView;
import com.itboye.jmsfhz.widget.XListView.IXListViewListener;

public class MeShouDaoActivity extends BaseActivity implements Observer,
		IXListViewListener {
	XListView meshoduaolistview;
	MeTouAdapter adapter;
	MeTouGuoDeBean bean;
	Projectpresenter projectpresenter;
	String uid;
	TextView tvJianli;
	int page_num = 1;
	int page_size = 10;
	ImageView reback;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.meshoudaoactivity;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		showProgressDialog("数据加载中...", true);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		bean = new MeTouGuoDeBean();
		projectpresenter = new Projectpresenter(this);
		bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		bean.setUid(uid);
		projectpresenter.getTou(uid);
		meshoduaolistview.setXListViewListener(this);
		/** 刷新后获取数据 **/
		meshoduaolistview.setPullLoadEnable(true);
		/** 刷新 **/
		meshoduaolistview.setPullRefreshEnable(true);
		reback.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity.getEventType() == projectpresenter.meTouSuccessEvent) {
			bean = (MeTouGuoDeBean) entity.getData();
			if (bean == null) {
				tvJianli.setVisibility(View.VISIBLE);
				meshoduaolistview.setVisibility(View.GONE);
			} else
				meshoduaolistview.setVisibility(View.VISIBLE);
			tvJianli.setVisibility(View.GONE);
			adapter = new MeTouAdapter(this, bean);
			meshoduaolistview.setAdapter(adapter);
		}
		try {
			/**
			 * 当数据加载出来后dialog则消失
			 */
			closeProgressDialog();
		} catch (Exception e) {

		}

		stopLoad();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_num = 1;
		bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		page_size = 10;
		projectpresenter.getTou(uid);// (bean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_num++;
		bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		page_size++;
		projectpresenter.getTou(uid);
	}

	public void stopLoad() {
		meshoduaolistview.stopLoadMore();
		meshoduaolistview.stopRefresh();
	}

	class MeTouAdapter extends BaseAdapter {

		Context context;
		MeTouGuoDeBean list;

		public MeTouAdapter(Context context, MeTouGuoDeBean list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.getResult() == null ? 0 : list.getResult().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.getResult().get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@SuppressLint("SimpleDateFormat")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHouder houder;
			if (convertView == null) {
				houder = new ViewHouder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_emploment, null);
				houder.imgMessge = (ImageView) convertView
						.findViewById(R.id.imgMessge);
				houder.tvcompany = (TextView) convertView
						.findViewById(R.id.tvcompany);
				houder.tvAddree = (TextView) convertView
						.findViewById(R.id.tvAddree);
				houder.tvEmplomenttionTime = (TextView) convertView
						.findViewById(R.id.tvEmplomenttionTime);
				houder.imgauthentiaion = (ImageView) convertView
						.findViewById(R.id.imgauthentiaion);
				convertView.setTag(houder);
			} else {
				houder = (ViewHouder) convertView.getTag();
			}
			// if(list.getResult().get(position).getIs_auth_e().equals("0")){
			// houder.imgauthentiaion.setVisibility(View.GONE);
			// }else
			// if(list.getResult().get(position).getIs_auth_e().equals("1")){
			// houder.imgauthentiaion.setVisibility(View.VISIBLE);
			// }else
			houder.tvcompany.setText(list.getResult().get(position)
					.getEmploy_title());
			houder.tvAddree.setText(list.getResult().get(position)
					.getEmploy_address());
			houder.tvEmplomenttionTime.setText(new SimpleDateFormat(
					"yyyy-MM-dd").format(Long.parseLong(list.getResult()
					.get(position).getCreate_time()) * 1000));
			XImageLoader.load(
					GlobalConfig.JIUYE
							+ list.getResult().get(position).getEmploy_logo(),
					houder.imgMessge);

			return convertView;
		}

		class ViewHouder {
			ImageView imgMessge, imgauthentiaion;
			TextView tvcompany;
			TextView tvAddree;
			TextView tvEmplomenttionTime;
		}
	}

}
